//
//  ChangeInfoView.m
//  陶瓷
//
//  Created by 王戈 on 15/8/2.
//  Copyright (c) 2015年 王戈. All rights reserved.
//

#import "ChangeInfoView.h"

@interface ChangeInfoView () <UIActionSheetDelegate>

@property (nonatomic, strong) UIButton *cover;
@property (nonatomic, weak) IBOutlet UIButton *resetButton;

@end

@implementation ChangeInfoView

- (IBAction)btnTouchUpInside:(UIButton *)resetBtn
{
    if ([self.delegate respondsToSelector:@selector(btnResetPassword)]) {
        [self.delegate btnResetPassword];
    }
}

- (IBAction)sureToChange {
    [SVProgressHUD showWithStatus:@"正在更改用户信息 请稍后.."];
    self.isUploading = YES;
    
    [[TCNetworkTool sharedNetTool] uploadImage:self.iconButton.currentBackgroundImage andSuccessBlocks:^(NSString *successMessage) {
        
        [self sendChangeRequestWithURL:successMessage];
        
    } andFailureBlocks:^(NSString *failureMessage) {
        [SVProgressHUD dismiss];
        [MBHUDHelper showWarningWithText:failureMessage];
        self.isUploading = NO;
        if ([self.delegate respondsToSelector:@selector(disappearWithSuccess:)]) {
            [self.delegate disappearWithSuccess:NO];
        }
    }];
}

- (IBAction)logout:(id)butten
{
    [[TCNetworkTool sharedNetTool] logoutWithSuccessBlocks:^(NSString *successMessage) {
        if ([self.delegate respondsToSelector:@selector(logoutWithSuccess)]){
            [self.delegate logoutWithSuccess];
        }
    } andFailureBlocks:^(NSString *failureMessage) {
        [MBHUDHelper showWarningWithText:@"注销失败"];
        [self cancle];
    }];
}

- (IBAction)cancle {
    [self removeFromSuperview];
    if ([self.delegate respondsToSelector:@selector(disappearWithSuccess:)]) {
        [self.delegate disappearWithSuccess:NO];
    }
}

- (IBAction)takePhoto {
    UIActionSheet *as = [[UIActionSheet alloc] initWithTitle:@"更改头像" delegate:self cancelButtonTitle:@"取消" destructiveButtonTitle:@"拍照" otherButtonTitles:@"打开相册", nil];
    [as showInView:self.superview];
    
}

- (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex {
    if (buttonIndex == 1 || buttonIndex == 0) {
        if ([self.delegate respondsToSelector:@selector(photoChooseWithIndex:)]) {
            [self.delegate photoChooseWithIndex:buttonIndex];
        }
    }
}

- (void)sendChangeRequestWithURL:(NSString *)urlStr {
    
    [[TCNetworkTool sharedNetTool] userInfoEditWithHeadUrl:urlStr andNickName:_nameTextField.text andSuccessBlocks:^(NSString *successMessage) {
        [SVProgressHUD dismiss];
        [SVProgressHUD showSuccessWithStatus:@"更改成功!"];
        self.isUploading = NO;
        NSMutableDictionary *dict = [NSMutableDictionary dictionaryWithDictionary:[UserManager sharedUserManager].userModel.infoDict] ;
        [dict setValue:_nameTextField.text forKey:@"NickName"];
        [dict setValue:urlStr forKey:@"HeadImage"];
        
        [UserManager sharedUserManager].userModel.infoDict = dict;
        
        if ([self.delegate respondsToSelector:@selector(disappearWithSuccess:)]) {
            [self.delegate disappearWithSuccess:YES];
        }
        
        [self removeFromSuperview];
    } andFailureBlocks:^(NSString *failureMessage) {
        [SVProgressHUD dismiss];
        [MBHUDHelper showWarningWithText:failureMessage];
        self.isUploading = NO;
        if ([self.delegate respondsToSelector:@selector(disappearWithSuccess:)]) {
            [self.delegate disappearWithSuccess:NO];
        }
    }];
}

- (void)setInfoDict:(NSDictionary *)infoDict {
    _infoDict = infoDict;
    _nameTextField.text =  [infoDict objectForKey:@"NickName"];
}

@end
